package com.fly.service;

import com.fly.domain.Department;
import com.fly.mapper.DepartmentMapper;
import com.fly.repository.DepartmentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import reactor.core.publisher.Flux;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;

/**
 * @author Fly
 * @Description 部门Service
 * @Date Created in 15:30 2018/1/25
 * @Modified by
 */
@Service
public class DepartmentService extends CurdService<DepartmentRepository, Department> {

    @Autowired
    private DepartmentRepository departmentRepository;

    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 获取树数据
     * @param parentId
     * @return
     */
    public List<Department> getTreeData(Long parentId){
        return departmentMapper.getDepartmentsByParentId(parentId);
    }
    /**
     * 保存
     * @param department
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Department saveDepartment(Department department){
        return super.save(department);
    }

    /**
     * 根据名称查找
     * @param name
     * @return
     */
    public List<Department> findDepartmentsByName(String name){
        name = "%" + name + "%";
        return departmentRepository.findDepartmentsByNameLike(name);
    }

    /**
     * 删除部门
     * @param id
     */
    @Transactional(rollbackFor = Exception.class)
    public void removeDepartment(Long id){
        super.delete(id);
    }
}
